import { observable, action, toJS } from "mobx";
import { each } from 'lodash';
import { message } from 'antd';
import { TaskViewPageType } from "../../constants/task";
import API from '../../apis/cuspath';
import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export default class NodeFrom {
    @observable isInit = false;
    @observable labelCol = 6;
    @observable fieldCol = 18
    @observable id = '';
    @observable nameLabel = getLabel('502717', '节点名称')
    @observable name = '';
    @observable operatorSetType = '0';
    @observable resourceInfoVisible = false;
    @observable resourceidLabel = getLabel('502894', '操作者');
    @observable resouceid = '';
    @observable resourceObj = [];

    @observable hreflinkObjLabel = getLabel('502895', '表格');
    @observable hreflinkObj = [];
    @observable hreflinkObjOptions = [{
        key: TaskViewPageType.EXCEL,
        showname: getLabel('502895', '表格'),
        selected: true
    }, {
        key: TaskViewPageType.FORM,
        showname: getLabel('502896', '表单')
    }, {
        key: TaskViewPageType.CUSTOM,
        showname: getLabel('502465', '自定义')
    }
    , {
        key: TaskViewPageType.LOAD,
        showname: getLabel('514903', '路由地址')
    }

    ];

    @observable parentid = '';
    @observable parentName = '';

    @observable reportTimeLabel = getLabel('502898', '填报时间');
    @observable reportTime = '2';
    @observable reportTimeType = '1';

    @observable reportTypeVisible = false;
    @observable reportTypeLabel = getLabel('502899', '联合上报');
    @observable reportType = "1";

    @observable undoType = "0";
    @observable undoTypeLabel = getLabel('502900', '撤回方式');

    @observable synchronizeType = "0";
    @observable synchronizeTypeLabel = getLabel('510990','同步方式');
    @observable synchronizeNodeId = '';
    @observable synchronizeNodeObj = [];

    // 是否自动保存（1：自动保存）
    @observable autoSaveType = "0";
    @observable autoSaveTypeLabel = getLabel('512007','自动保存');

    @observable saveTimeTypeVisible = false;
    @observable saveTimeLabel = getLabel('512008','自动保存间隔');
    @observable saveTime = '1';
    @observable saveTimeType = '0';

    @observable sheetIds = '0';

    @observable joinCubeFlag = false;

    @observable isExcelPage = true;

    @observable isLoadPage = false;

    @observable countProcess = '0';

    @observable reportTimeSetting = {};

    @action init = (node, excelPages) => {
        this.isInit = true;
        this.hreflinkObjOptions1 = excelPages.map(d => ({ key: d.id, showname: d.name, sheetOptions : d.pagesheets}));
        each(node, action((value, key) => {
            this[key] = value;
            if (key === 'operatorSetType') {
                this.resourceInfoVisible = value != 1;
                if (!this.resourceInfoVisible) {
                    this.resourceObj = [];
                }
            } else if (key === 'autoSaveType') {
                this.saveTimeTypeVisible = value == 1;
            }else if(key === 'hreflinkObj'){
                const type = value ? value[0] : '';
                if(type==='EXCEL'){
                    this.isExcelPage = true;
                }else {
                    this.isExcelPage = false;
                }
                if(type==='LOAD'){
                    this.isLoadPage = true;
                }else {
                    this.isLoadPage = false;
                }
            }else if(key==='reportTimeSetting'){
                if(value) {
                    this.reportTimeSetting = JSON.parse(value);
                }else{
                    this.reportTimeSetting = {type:'default',timeType:'year',week:'0'};
                }
            }
        }));    
    }

    @action change = (key, value, names, obj) => {
        this[key] = value;
        if (key === 'resourceid') {
            this.resourceObj = obj;
        } else if (key === 'autoSaveType') {
            this.saveTimeTypeVisible = value == 1;
        } 
         else if (key === 'synchronizeNodeId'){
            this.synchronizeNodeObj = obj;
        } else if (key === 'hreflinkObj'){
            if(value[0]==='EXCEL'){
                this.isExcelPage = true;
            }else {
                this.autoSaveType = '0';
                this.saveTimeTypeVisible = false;
                this.saveTime = '1';
                this.saveTimeType = '0';
                this.isExcelPage = false;
            }
            if(value[0]==='LOAD'){
                this.isLoadPage = true;
            }else {
                this.isLoadPage = false;
            }
        } 
    }
    toJSON = () => {
        return {
            operatorSetType: this.operatorSetType,
            resourceid: this.resourceObj.map(d => d.id).join(','),
            id: this.id,
            name: this.name,
            hreflink: JSON.stringify(this.hreflinkObj),
            reportTime: this.reportTime,
            reportTimeType: this.reportTimeType,
            reportType:this.reportType,
            undoType:this.undoType, 
            synchronizeType:this.synchronizeType,
            synchronizeNodeId:this.synchronizeNodeId,
            reportType: this.reportType,
            autoSaveType: this.autoSaveType,
            saveTime: this.saveTime,
            saveTimeType: this.saveTimeType,
            sheetIds: this.sheetIds,
            countProcess:this.countProcess,
            reportTimeSetting:JSON.stringify(this.reportTimeSetting)
        };
    }
    bind = (key, obj) => {
        return {
            value: this[key],
            valueObj: this[obj],
            onChange: this.change.bind(this, key),
            label: this[`${key}Label`],
            labelCol: this[`${key}LabelCol`] || this.labelCol,
            fieldCol: this[`${key}FieldCol`] || this.fieldCol,
            options: this[`${key}Options`],
            options1: this[`${key}Options1`],
        }
    }
}